/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;

namespace EXtremecode.PresentationLayer
{
	/// <summary>
	/// Summary description for Pager.
	/// </summary>
	public struct PageLinkClickEventArgs
	{
		int pageNumber;
		int pageSize;
		public PageLinkClickEventArgs(int pageNumber,int pageSize)
		{
			this.pageNumber = pageNumber;
			this.pageSize = pageSize;
		}
		public int PageNumber
		{
			get
			{
				return this.pageNumber;
			}
		}
		public int PageSize
		{
			get
			{
				return this.pageSize;
			}
		}
	}
	public delegate void PageLinkClickDelegate(PageLinkClickEventArgs e);
	public interface IPager
	{
		event PageLinkClickDelegate PageLinkClick;
		int TotalPages
		{
			get;
		}

		long TotalRecords
		{
			get;
			set;
		}

		int PageSize
		{
			get;
			set;
		}

		int CurrentPageNumber
		{
			get;
			set;
		}

		int NumberOfDisplayPages
		{
			get;
			set;
		}

		string PageSizesList
		{
			get;
			set;
		}

	}
	public class Pager
	{
		public static void GetFirstAndLastDisplayPageNumbers(int currentPageNum, int totalPages, int numOfDisplayPages,
			out int firstDisplayPageNumber, out int lastDisplayPageNumber)
		{
			int numOfPagesAheadCurrentLink = numOfDisplayPages / 2;
			int numOfPagesBehindCurrentLink = (numOfDisplayPages / 2) - 1 + (numOfDisplayPages % 2);

			lastDisplayPageNumber = currentPageNum + numOfPagesAheadCurrentLink;
			firstDisplayPageNumber = currentPageNum - numOfPagesBehindCurrentLink;

			//************for example***********
			//numOfPagesLinks=11
			//curentPageNum=7;
			//numOfPagesAheadCurrentLink = 11/2 = 5
			//numOfPagesBehindCurrentLink = 11/2 - 1 + (11%2) = 5;
			//***subtract by one,to include the current page num.*** 
			//1 [2 3 4 5 6 (7) 8 9 10 11 12] 13 14 15
			//**********************************


			//----adjustment----
			//if lastPageNumer is greater then total pages we need to adjust the number,
			//by which lastPageNum exceeded
			if (totalPages < lastDisplayPageNumber)
			{
				int diff = lastDisplayPageNumber - totalPages;
				firstDisplayPageNumber = firstDisplayPageNumber - diff;
				lastDisplayPageNumber = totalPages;
			}

			if (firstDisplayPageNumber < 1)
			{
				int diff = 1 - firstDisplayPageNumber;
				lastDisplayPageNumber = (diff + lastDisplayPageNumber > totalPages) ?
				totalPages : diff + lastDisplayPageNumber;
				firstDisplayPageNumber = 1;
			}
		}

	}
}
